clc;
clear all;
 
 %Home first
%  x = [-4.5; 14; -4.5; 14];
%  y = [-18; -18; -6; -6];
%  orignal=imread('newpic.jpg');

  x = [0;151.5;0;97];
  y = [-513.5;-513.5;-256.0;-256.0];
  orignal=imread('facultySecond.jpg');

%  Home 2nd
%   x = [-6; 6; -6; 6];
%   y = [-36.5; -30; -24.5; -18];
%   orignal=imread('mypicnew.jpg');

% originol x = [0; 0; 106; 106];
%  y = [0; 60; 0; 60];
  
% change x = [60; 0; 60; 0];
 % y = [0; 0; 106; 106];

 %principle office first
 
% x=[-7,7.5;-6.10,6];
% y=[-30.5,-43.9,-20,-27.5];
 
 
% orignal=imread('image.jpg');
 
 %principle office 2nd 
 
%         x=[0;97;0;81.5];
%         y=[-256.0;-256.0;-177.5;-177.5];
 
%        x=[-63;151.5;-97;97];
%        y=[-616.5;-513.5;-242.5;-256.0];


 
%orignal=imread('facultySecond.jpg');
 
   figure, imshow(orignal);
% % % disp 'select points required and press enter';
   [xd yd]=getpts();
   xd
   yd
   save('myfile.mat', 'orignal', 'x', 'y', 'xd', 'yd');
%  
%  
load('myfile.mat');

m = zeros(8, 9);
for i=1:4
    m(2*i-1,:)= [x(i),y(i),1,0,0,0, -x(i)*xd(i),-xd(i)*y(i),-xd(i)];
  m(2*i, :)= [0,0,0,x(i),y(i),1, -x(i)*yd(i),-y(i)*yd(i),-yd(i)];
end;

H1 = null(m);
H1 = H1/H1(9);
H = reshape(H1,3,3);

  tform = maketform('projective',inv(H));
    resimg = imtransform(orignal, tform, 'size', size(orignal));
 % 
  subplot(2,1,1), imshow(orignal,[]);
  subplot(2,1,2), imshow(resimg,[]);

 
% map to real word coordinates

 figure, imshow(orignal);
% 
 [xp yp]=ginput(1);
% 
% 
% % loop for length of xp
xp
yp
  len=size(xp);
% 
%  
  r = [xp; yp; 1];
  r
%b=reshape([xp yp],3,3);
 %  
%  1.
%   inver=inv(H);
%     inver
%     ht=inver * r; 
%     ht1=ht/ht(3);

% 2. 
    inver=inv(H);
    inver
    ht=H * r;
    ht1 = ht / ht(3);

 % 3.  
%    inver=inv(H);
%    inver
%    ht=H * r;
%   ht1 = inver / ht(3);
 

%  inver=inv(H);
%    inver
%    ht=inver * r;
% 
%   ht1 = inver / ht(3);


  
  % % printf(ht);
 ht1

 
% %h = reshape(H,4,4);
% %d=h.*[xp yp];
% %printf(d);
% 
%  xr=[0;0;0;0];
%  yr=[0;0;0;0];
% 
% d = H\r;
% %printf(d);
% d
% %xr, yr from inv(h))*xy,yp
% 
% %camera location calculation
% 
% myfile.mat
